/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package broad.dao;


import broad.bean.Categoria;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

/**
 *
 * @author Rafael
 */
public class DAOCategoria extends DAOGenerics<Categoria, Integer> {

    @Override
    public Categoria buscar(Integer obj) {
        SessionFactory sf = new AnnotationConfiguration().configure().buildSessionFactory();
        Session s = sf.openSession();

        try {
            return buscar(obj, s);

        } finally {
            s.close();
        }

    }

    @Override
    public Categoria buscar(Integer obj, Session s) {
        String sql = "from Categoria where idCategoria = :id";

        Query qr = s.createQuery(sql);
        qr.setParameter("id", obj);

        return (Categoria) qr.uniqueResult();
    }

    @Override
    public List<Categoria> buscarTodos() {
        SessionFactory sf = new AnnotationConfiguration().configure().buildSessionFactory();
        Session s = sf.openSession();

        try {
            return buscarTodos(s);

        } finally {
            s.close();
        }
    }

    @Override
    public List<Categoria> buscarTodos(Session s) {
        String sql = "from Categoria";

        Query qr = s.createQuery(sql);

        return (List<Categoria>) qr.list();
    }
    
    public List<Categoria> buscarNome(String obj) {
        SessionFactory sf = new AnnotationConfiguration().configure().buildSessionFactory();
        Session s = sf.openSession();

        try {
            return buscarNome(obj, s);

        } finally {
            s.close();
        }
    }

    public List<Categoria> buscarNome(String obj, Session s) {
        String sql = "from Categoria where nome = :nome";

        Query qr = s.createQuery(sql);
        qr.setParameter("nome", obj);

        return (List<Categoria>) qr.list();
    }    
}